package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsInspectManage;
import com.ruoyi.system.domain.news.dto.NewsInspectManageDto;
import com.ruoyi.system.domain.news.query.NewsInspectManageQuery;
import com.ruoyi.system.domain.news.vo.NewsInspectManageVo;
import com.ruoyi.system.service.news.NewsInspectManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsInspectManage")
@Api(value = "newsInspectManageController", tags  = "监督管理")
public class NewsInspectManageController extends BaseController {

	@Resource
	private NewsInspectManageService newsInspectManageService;

    @ApiOperation(value = "新增/修改监督管理信息", notes = "新增/修改监督管理信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsInspectManageDto newsInspectManageDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsInspectManage newsInspectManage = new NewsInspectManage();
		BeanUtils.copyProperties(newsInspectManageDto,newsInspectManage);
		if (newsInspectManage.getId()==null) {
			newsInspectManage.setCreateBy(loginUser.getUsername());
			newsInspectManage.setDeptId(loginUser.getDeptId());
			newsInspectManage.setCreateTime(new Date());
			newsInspectManage.setUpdateBy(loginUser.getUsername());
			newsInspectManage.setUpdateTime(new Date());
		} else {
			newsInspectManage.setUpdateBy(loginUser.getUsername());
			newsInspectManage.setUpdateTime(new Date());
		}
		boolean result = newsInspectManageService.saveOrUpdate(newsInspectManage);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除监督管理信息", notes = "删除监督管理信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsInspectManageService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个监督管理信息", notes = "获取单个监督管理信息")
	@GetMapping("/getById")
	public R<NewsInspectManageVo> getById(@RequestParam(value = "id") Long id) {
		NewsInspectManageQuery newsInspectManageQuery = new NewsInspectManageQuery();
		newsInspectManageQuery.setId(id);
		List<NewsInspectManageVo> newsInspectManageList = newsInspectManageService.getNewsInspectManageList(newsInspectManageQuery);
		if (newsInspectManageList!=null&&newsInspectManageList.size()>0) {
			return R.ok(newsInspectManageList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取监督管理信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsInspectManageVo>> list(@RequestBody NewsInspectManageQuery newsInspectManageQuery) {
		List<NewsInspectManageVo> newsInspectManageList = newsInspectManageService.getNewsInspectManageList(newsInspectManageQuery);
		return R.ok(new PageInfo<>(newsInspectManageList));
	}
}
